<?php
header('Access-Control-Allow-Origin: *');
header('Access-Control-Allow-Headers: *');
$root = $_SERVER['DOCUMENT_ROOT'] . "/php/";
include_once "$root/class/AppInfo.php";
include_once "$root/class/CardKeyManager.php";
include_once "$root/class/CardKey.php";
if (empty($_GET["type"])) {
    echo "参数错误";
    return;
}
$cardKeyManager = new CardKeyManager();
$appInfo = new AppInfo();
switch ($_GET["type"]) {

    case "ak":
        if (!is_numeric($_GET["k_num"]) || !is_numeric($_GET["k_time"])) {
            echo "格式错误";
            return;
        }
        $kNum = $_GET["k_num"];
        $kTime = $_GET["k_time"];
        for ($i = 0; $i < $kNum; $i++) {
            $cardKey = new CardKey(md5(rand(1000, 9000)), (int)$kTime, false);
            $cardKeyManager->addCardKey($cardKey);
        }
        echo "添加成功";
        break;
    case "rk":
        if ($cardKeyManager->removeCardKey($_GET["kid"])){
            echo "删除成功";
        } else {
            echo "删除失败";
        }
        break;
    case "gk":
        echo json_encode($cardKeyManager->getAllCardKey());
        break;
    case "an":
        if (empty($_POST["c"]) || empty($_POST["tag"])) {
            echo "参数不能为空";
            return;
        }
        $appInfo->addAppNotice($_POST["c"], $_POST["tag"]);
        if ($appInfo->build()) {
            echo "添加成功";
        } else {
            echo "添加失败";
        }
        break;
    case "rn":
        if (!is_numeric($_GET["nid"])) {
            echo "参数格式错误";
            return;
        }
        $appInfo->removeAppNotice($_GET["nid"]);
        if ($appInfo->build()) {
            echo "删除成功";
        } else {
            echo "删除失败";
        }
        break;

    case "gn":
        echo json_encode($appInfo->getAppNotices());
        break;
    default:
}